//
//  BmiDetailViewController.m
//  WeightMaster
//
//  Created by maoyu on 15/8/21.
//  Copyright (c) 2015年 buddysoft. All rights reserved.
//

#import "BmiDetailViewController.h"
#import "WMModels.h"
#import "BDFoundation.h"
#import "UIColor+WeightMaster.h"
#import <Masonry.h>
#import <EXTScope.h>

@interface BmiDetailViewController () {
    UIView * _colorBarView;
    CGFloat _bmi;
}

@end

@implementation BmiDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"BMI指数";

    [self initBmiDescriptionLabel];
    [self initBmi];
    
    [self createColorBarView];
    [self createCircle];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


#pragma 私有函数
- (void)initBmiDescriptionLabel {
    NSString * description = @"BMI指数是目前国际上常用的衡量人体胖瘦程度以及是否健康的标准，本软件使用的是适合亚洲人体质的标准，是中立而可靠的指标。";
    NSMutableAttributedString * attrDescription = [NSMutableAttributedString attributedStringWithString:description];
    [attrDescription setFont:[UIFont systemFontOfSize:14]];
    [attrDescription setTextColor:RGB(82, 82, 95)];

    [attrDescription modifyParagraphStylesWithBlock:^(OHParagraphStyle *paragraphStyle) {
        paragraphStyle.textAlignment = kCTTextAlignmentLeft;
        paragraphStyle.lineSpacing = 5.f;
    }];
    
    self.descriptionLabel.text = description;
    self.descriptionLabel.attributedText = attrDescription;
}

- (void)initBmi {
    NSNumber * weight = [WMDataCache sharedInstance].latestWeight.value;
    NSNumber * height = [WMAppSetting sharedInstance].height;
    _bmi = [[WMHelper BMIForWeight:weight height:height] floatValue];
    
    self.bmiLabel.text = [NSString stringWithFormat:@"%.1f",_bmi];
}

- (void)createColorBarView {
    const CGFloat height = 35.0;
    
    _colorBarView = [[UIView alloc] init];
    [self.view addSubview:_colorBarView];
    @weakify(self)
    [_colorBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.width.equalTo(self.view);
        make.top.equalTo(self.backgroundImageView.mas_bottom);
        make.height.mas_equalTo(height);
    }];
    
    CAGradientLayer * colorBarLayer = [CAGradientLayer layer];
    colorBarLayer.frame = CGRectMake(0,0,kScreen_Width,height);
    colorBarLayer.startPoint = CGPointMake(0, 0);
    colorBarLayer.endPoint = CGPointMake(1, 0);
    colorBarLayer.colors = @[(__bridge id)RGB(51, 207, 244).CGColor,
                              (__bridge id)RGB(53, 255, 50).CGColor,
                              (__bridge id)RGB(255, 252, 0).CGColor,
                              (__bridge id)RGB(255, 157, 0).CGColor,
                              (__bridge id)RGB(255, 62, 45).CGColor];
    
    [_colorBarView.layer addSublayer:colorBarLayer];
    
    // 画色带标题
    NSArray * titles = kBMI_LEVEL;
    NSInteger count = [titles count];
    UILabel * label;
    CGFloat width = kScreen_Width/count;
    for (NSInteger index = 0; index < count; index++) {
        label = [[UILabel alloc] init];
        label.text = [titles objectAtIndex:index];
        label.font = [UIFont systemFontOfSize:15];
        label.textColor = RGB(32, 32, 47);
        label.textAlignment = NSTextAlignmentCenter;
        [_colorBarView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            @strongify(self)
            make.width.mas_equalTo(width);
            make.height.equalTo(self->_colorBarView);
            make.top.equalTo(self->_colorBarView);
            make.leading.equalTo(_colorBarView.mas_left).offset(width*index);
        }];
    }
    
    // 画色带分隔线及关键值
    NSArray * points = kBMI_POINT;
    count--;
    UIImageView * imageView;
    for (NSInteger index = 1; index <= count; index++) {
        imageView = [[UIImageView alloc] init];
        imageView.image = [UIImage imageNamed:@"v_line"];
        [self.view addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            @strongify(self)
            make.width.mas_equalTo(3);
            make.height.mas_equalTo(18);
            make.leading.equalTo(self.view.mas_leading).offset(width*index);
            make.bottom.equalTo(self->_colorBarView.mas_top).offset(3);
        }];
        
        label = [[UILabel alloc] init];
        label.text = [[points objectAtIndex:index - 1] stringValue];
        label.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:25.0];
        label.textColor = RGB(255, 255, 255);
        label.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            @strongify(self)
            make.width.mas_equalTo(50);
            make.leading.equalTo(self.view.mas_leading).offset(width*index - 20);
            make.bottom.equalTo(self->_colorBarView.mas_top).offset(-18-5);
        }];
    }
}

- (void)createCircle {
    if (_bmi < 0) {
        _bmi = 0.0;
    }
    if (_bmi > 45.0) {
        _bmi = 45.0;
    }
    
    NSInteger count = [kBMI_LEVEL count];
    CGFloat separatorWidth = kScreen_Width/count;
    NSInteger index = 0;
    CGFloat offset = 0.0;
    for (NSNumber * point in kBMI_POINT) {
        if (_bmi <= [point floatValue]) {
            if (0 == index) {
                offset += _bmi*separatorWidth/[point floatValue];
            }else {
                offset += (_bmi-[[kBMI_POINT objectAtIndex:(index -1)] floatValue])*separatorWidth/([[kBMI_POINT objectAtIndex:index] floatValue]- [[kBMI_POINT objectAtIndex:(index -1)] floatValue]);
            }
            break;
        }else {
            offset += separatorWidth;
        }
        
        index++;
    }
    
    CGFloat const circleWidth = 16.0;
    UIView * bmiView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, circleWidth, circleWidth)];
    bmiView.backgroundColor = [UIColor mainColor];
    [bmiView makeCircle];
    [bmiView addBorderWithWidth:1 withColor:[UIColor whiteColor]];
    [self.view addSubview:bmiView];
    @weakify(self)
    [bmiView mas_makeConstraints:^(MASConstraintMaker *maker) {
        @strongify(self)
        maker.width.mas_equalTo(circleWidth);
        maker.height.mas_equalTo(circleWidth);
        maker.leading.equalTo(self->_colorBarView).offset(offset);
        maker.bottom.equalTo(self->_colorBarView.mas_top).offset(circleWidth/2.0);
    }];
    
    [self initBmiLevel:index];
}

- (void)initBmiLevel:(NSInteger)level {
    self.levelLabel.text = [NSString stringWithFormat:@"体型%@",[kBMI_LEVEL objectAtIndex:level]];
}

@end
